package com.szholly.plug.form.upload;

import java.util.HashMap;

import org.springframework.stereotype.Component;

import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.data.general.*;
import com.szholly.data.general.metadata.IBaseTable;

/**
 * 附件表
 */
@Component("FORM_DOCMENT")
public class FormDocmentTable extends InterceptorTableBase  implements IBaseTable{

	public final static String TABLE_NAME = "FORM_DOCMENT";
	
	/**
	 * single ref
	 */
	public static FormDocmentTable getSingleRef() {
		return (FormDocmentTable) SpringBeanFactory.getBean("FORM_DOCMENT");
	}

	@Override
	public String getMetadataTableName() {
		return TABLE_NAME;
	}

	private HashMap<String, Field> fields;

	@Override
	protected HashMap<String, Field> fields() {
		if (fields == null) {
			fields = new HashMap<String, Field>();

			Field itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_GUID);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(1);
			itemField.setFieldAliasName("GUID");
			itemField.setCanShow(false);
			itemField.setIsKey(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_VERSION);
			itemField.setDataType(GlobalDataType.Int32);
			itemField.setFieldID(2);
			itemField.setFieldAliasName("VERSION");
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_COMPANY);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(3);
			itemField.setFieldAliasName("COMPANY");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_SYSTEMNAME);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(4);
			itemField.setFieldAliasName("SYSTEMNAME");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormDocmentRow.FIELD_DUpsize);
			itemField.setDataType(GlobalDataType.Int64);
			itemField.setFieldID(5);
			itemField.setFieldAliasName("文档大小");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormDocmentRow.FIELD_SName);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(6);
			itemField.setFieldAliasName("文档名称");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormDocmentRow.FIELD_SUptime);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(7);
			itemField.setFieldAliasName("上传时间");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormDocmentRow.FIELD_SType);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(8);
			itemField.setFieldAliasName("文档类型");
			itemField.setCanShow(false);
			itemField.setDomain(FileTypeDic.getDomain());
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormDocmentRow.FIELD_SUpip);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(9);
			itemField.setFieldAliasName("上传IP");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormDocmentRow.FIELD_SUpuser);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(10);
			itemField.setFieldAliasName("上传人");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormDocmentRow.FIELD_SUrl);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(11);
			itemField.setFieldAliasName("文档URL");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormDocmentRow.FIELD_SLpath);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(12);
			itemField.setFieldAliasName("逻辑目录");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormDocmentRow.FIELD_PLPATH);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(12);
			itemField.setFieldAliasName("父目录");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormDocmentRow.FIELD_JB);
			itemField.setDataType(GlobalDataType.Int32);
			itemField.setFieldID(9);
			itemField.setFieldAliasName("机构级别");
			itemField.setCanShow(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(FormDocmentRow.FIELD_ISCHILD);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(8);
			itemField.setFieldAliasName("是否为叶子节点");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
		}
		return fields;
	}

	@Override
	public String getMetadataTableAliasName() {
		return "附件管理表";
	}

	@Override
	public String getTableGroup() {
		return "表单信息";
	}
	
	@Override
	public String getCodeFieldName() {
		return FormDocmentRow.FIELD_SName;
	}

	@Override
	public String getCaptionFieldName() {
		return FormDocmentRow.FIELD_SUrl;
	}
	
	/**
	 * 获取级别字段名
	 */
	@Override
	public String getLevelFieldName(){
		return FormDocmentRow.FIELD_JB;
	}
	
	/**
	 * 获取是否为叶子节点字段名
	 */
	@Override
	public String getIsChildFieldName() {
		return FormDocmentRow.FIELD_ISCHILD;
	}

	@Override
	public String getIDFieldName() {
		return FormDocmentRow.FIELD_SLpath;
	}

	@Override
	public String getPIDFieldName() {
		return FormDocmentRow.FIELD_PLPATH;
	}
	
	@Override
	public String getOrderFieldName() {
		return FormDocmentRow.FIELD_SUptime;
	}

	@Override
	public boolean getIsCodeTable() {
		return true;
	}
	
	@Override
	public boolean getIsTreeTable() {
		return true;
	}

	@Override
	public boolean getIsManageCodeTable() {
		return false;
	}
	
	@Override
	public String getGUIDFieldName(){
		return FormDocmentRow.FIELD_GUID;
	}
	
	public void SaveEntityInThread(FormDocmentRow entity){
		DocmentSaveThread logThread = new DocmentSaveThread(this, entity);
		logThread.start();
	}
	
	public void SaveEntityInThread2(FormDocmentRow entity){
		DocmentSaveThread2 logThread = new DocmentSaveThread2(this, entity);
		logThread.start();
	}
	
	private class DocmentSaveThread2 extends Thread {

		private FormDocmentTable logService;
		private FormDocmentRow entity;
		
		public DocmentSaveThread2(FormDocmentTable logService, FormDocmentRow entity){
			this.logService = logService;
			this.entity = entity;
		}
		
		@Override
		public void run() {
			IQueryMap map = new QueryMap();
			map.getWhereMap().put(FormDocmentRow.FIELD_SLpath, entity.getSLpath());
			map.getWhereMap().put(FormDocmentRow.FIELD_PLPATH, entity.getPLpath());
			FormDocmentRow row = logService.getEntity(FormDocmentRow.class, map);
			if(row==null){
				logService.save(entity);
			}
		}
	}
	
	private class DocmentSaveThread extends Thread {

		private FormDocmentTable logService;
		private FormDocmentRow entity;
		
		public DocmentSaveThread(FormDocmentTable logService, FormDocmentRow entity){
			this.logService = logService;
			this.entity = entity;
		}
		
		@Override
		public void run() {
			logService.save(entity);
		}
	}
}